package com.polaris.live.popup

import android.content.Context
import android.view.View
import com.polaris.live.R
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.PopupLiveSelectMoreCardBinding
import com.polaris.live.net.liveService
import com.polaris.live.popup.base.BaseLiveBottomPopupView
import com.polaris.live.resp.back_resp.BanMessageResp
import com.polaris.live.resp.back_resp.InfoOtherResp

/**
 * 直播间通用更多卡片
 *
 * @author Created by 半仙 on 2024/5/11/011 14:32
 */
class LiveSelectMoreCardPopup(
    context: Context,
    private val mViewModel: BaseViewModel,
    private val mInfoOtherResp: InfoOtherResp,
    private val mObjectId: Long?,
    private val mHomeOwnerId: Long?,
    private val mBanMessageUser: List<Long>?,
    private val mIsCall: Boolean,
    private val mOnKickRoomListener: (() -> Unit)? = null,
    private val mOnAtClickListener: (() -> Unit)?,
) : BaseLiveBottomPopupView<PopupLiveSelectMoreCardBinding>(
    context,
    { PopupLiveSelectMoreCardBinding.bind(it) }
) {

    override fun getImplLayoutId() = R.layout.popup_live_select_more_card

    override fun getContentView(): View = mBinding.bottom

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupLiveSelectMoreCardBinding.bind(popupImplView)

        mBinding.tvAtText.text = mInfoOtherResp.nickname

        //判断是否禁言
        val isBanMessage = mBanMessageUser?.any { userId -> userId == mInfoOtherResp.userId } ?: false
        setBanUi(isBanMessage)

        if (mHomeOwnerId != UserManager.getUserId()) {
            mBinding.llBanMessage.visibility = View.GONE
            mBinding.llKickOut.visibility = View.GONE
        }
        //当我是主播  并且弹出的是我的更多卡片则
        if (mHomeOwnerId == mInfoOtherResp.userId || mInfoOtherResp.userId == UserManager.getUserId()) {
            mBinding.llBanMessage.visibility = View.GONE
            mBinding.llKickOut.visibility = View.GONE
        }
        if (mIsCall) {
            mBinding.llBanMessage.visibility = View.GONE
            mBinding.llAtIt.visibility = View.GONE
            mBinding.llKickOut.visibility = View.GONE
        }

        mBinding.llAtIt.setOnSingleClickListener {
            mOnAtClickListener?.invoke()
            dismiss()
        }
        mBinding.llKickOut.setOnSingleClickListener {
            mOnKickRoomListener?.invoke()
            dismiss()
        }
        mBinding.llReportIt.setOnSingleClickListener {
            GlobalPopupManager.showReport(
                context,
                mInfoOtherResp.nickname,
                mViewModel,
                mInfoOtherResp.userId,
                mObjectId
            )
        }

        //禁言
        mBinding.banMessageSwitchBtn.setOnCheckedChangeListener { buttonView, isChecked ->
            if (buttonView.isPressed) {
                banMessage(mInfoOtherResp.userId, isChecked) {
                    mBinding.banMessageSwitchBtn.isChecked = it
                }
            }
        }
    }

    /**
     * 公屏禁言
     */
    private fun banMessage(userid: Long, ban: Boolean, onSetBanMessage: ((Boolean)) -> Unit) {
        mViewModel.requestLoading(
            { liveService.banMessage(BanMessageResp(userid, ban, mObjectId)) }, {
                if (it.ban) {
                    ToastGravity.showGravity(com.polaris.live.translation.R.string.public_screen_ban)
                } else {
                    ToastGravity.showGravity(com.polaris.live.translation.R.string.public_screen_unban)
                }
                setBanUi(it.ban)
                onSetBanMessage(it.ban)
            },
            {
                onSetBanMessage(!ban)
                ToastGravity.showGravity(it.errorMsg)
            }
        )
    }

    private fun setBanUi(isBan: Boolean) {
        mBinding.root.post {
            if (isBan) {
                mBinding.ivBanMessage.alpha = 1.0f
                mBinding.tvBanMessage.alpha = 1.0f
            } else {
                mBinding.ivBanMessage.alpha = 0.4f
                mBinding.tvBanMessage.alpha = 0.4f

            }
            mBinding.banMessageSwitchBtn.isChecked = isBan
        }
    }
}